import {
    CHANGE_CATEGORY,
    CHANGE_CATEGORY_SONGS,
    CHANGE_CURRENT_CATEGORY,
    PER_PAGE_NUMBER
} from "./constants";

import {
    getSongCategory,
    getCategorySongList
} from "@/services/songs";
import { 
    handleSongsCategory
  } from "@/utils/handle-data";

export const changeCategoryAction = (categoryList) => {
    return {
        type: CHANGE_CATEGORY,
        categoryList: categoryList
    }
}

export const changeCategorySongsAction = (res) => {
    return {
        type: CHANGE_CATEGORY_SONGS,
        categorySongs: res
    }
}

export const changeCurrentCategoryAction = (name) => ({
    type: CHANGE_CURRENT_CATEGORY,
    currentCategory: name
})


//分类数据请求接口 action
export const getCategoryAction = () => {
    return dispatch => {
        getSongCategory().then(res => {
            const categoryData = handleSongsCategory(res);
            dispatch(changeCategoryAction(categoryData))
        })
    }
}

//某分类下的歌手数据接口 action
export const getCategorySongsAction = (page) => {
    return (dispatch, getState) => {
        const cat = getState().getIn(["songs", "currentCategory"]);
        getCategorySongList(cat, page, PER_PAGE_NUMBER).then(res => {
            dispatch(changeCategorySongsAction(res))
        })
    }
}